import type { App } from "vue";
import {
  createRouter,
  createWebHashHistory,
  type RouteRecordRaw,
  type NavigationGuardNext,
  type RouteLocationNormalized,
} from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/",
    component: () => import("@/views/home/home.vue"),
    meta: { hidden: true },
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },
  {
    path: "/empty",
    component: () => import("@/views/pages/empty.vue"),
    meta: { hidden: true },
  },
  {
    path: "/index",
    component: Layout,
    redirect: "/index",
    children: [
      {
        path: "/index",
        component: () => import("@/views/home/index.vue"),
        name: "Home",
        meta: { title: "首页", nav: true, footer: true, type: "user" },
      },
      {
        path: "/invoice1",
        component: () => import("@/views/home/invoice1.vue"),
        name: "Invoice1",
        meta: { title: "发票管理", nav: false, type: "company" },
      },
      {
        path: "/invoice2",
        component: () => import("@/views/home/invoice2.vue"),
        name: "Invoice2",
        meta: { title: "发票查询统计", nav: false, type: "company" },
      },
      {
        path: "/pages/a6",
        component: () => import("@/views/pages/a6.vue"),
        meta: { title: "申报信息查询", nav: false, type: "user" },
      },
      {
        path: "/pages/a7",
        component: () => import("@/views/pages/a7.vue"),
        meta: { title: "申报信息查询详情", nav: false, type: "user" },
      },
      {
        path: "/pages/a8",
        component: () => import("@/views/pages/a8.vue"),
        meta: { title: "申报信息查询详情", nav: false, type: "user" },
      },
      {
        path: "/pages/a9",
        component: () => import("@/views/pages/a9.vue"),
        meta: { title: "申报信息查询详情", nav: false, type: "user" },
      },
      {
        path: "/pages/a10",
        component: () => import("@/views/pages/a10.vue"),
        meta: { title: "申报信息查询详情", nav: false, type: "user" },
      },
      {
        path: "/pages/a14",
        component: () => import("@/views/pages/a14.vue"),
        meta: { title: "全量发票查询", nav: false, type: "company" },
      },
      {
        path: "/pages/a15",
        component: () => import("@/views/pages/a15.vue"),
        meta: { title: "财务报表信息查询", nav: false, type: "user" },
      },
      {
        path: "/pages/a16",
        component: () => import("@/views/pages/a16.vue"),
        meta: { title: "财务报表信息查询详情", nav: false, type: "user" },
      },
      {
        path: "/pages/a17",
        component: () => import("@/views/pages/a17.vue"),
        meta: { title: "财务报表信息查询详情", nav: false, type: "user" },
      },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

// 添加全局导航守卫
router.beforeEach(
  (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
    // 获取当前时间戳
    const timestamp = Date.now().toString();

    // 从缓存中获取 token 并截取中间 20 位
    const token = localStorage.getItem("TOKEN_KEY") || "1234567890";
    let k = "";
    if (token) {
      const start = Math.max(0, Math.floor((token.length - 20) / 2));
      k = token.substring(start, start + 20);
    }

    // 将“国家税务总局山东省电子税务局”进行 Base64 编码
    const str = "国家税务总局山东省电子税务局";
    const utf8Bytes = encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
      return String.fromCharCode(("0x" + p1) as any);
    });
    const n = btoa(utf8Bytes);

    // 将时间戳进行 Base64 编码
    const t = btoa(timestamp);

    // 将 k 进行 Base64 编码
    const kBase64 = btoa(k);

    // 检查当前路由是否已经包含时间戳参数
    if (
      !to.query.t ||
      to.query.t !== t ||
      !to.query.k ||
      to.query.k !== kBase64 ||
      !to.query.n ||
      to.query.n !== n
    ) {
      // 创建新的查询参数对象，包含时间戳、k 和 n
      const newQuery = {
        ...to.query,
        t: t,
        k: kBase64,
        n: n,
      };

      // 重定向到新的 URL，包含时间戳参数
      return next({ ...to, query: newQuery });
    }

    // 继续导航
    next();
  }
);

export default router;
